/**
 * 评论框类
 */
MPT.addClass('Publish', function() {

    var comment_jQ, input_jQ, remain_jQ, publish_jQ, count_jQ, cancel_jQ, op_emotion_jQ, op_topic_jQ, op_friend_jQ, emotion_jQ, topic_jQ, friend_jQ, submit_jQ, insert_jQ, search_jQ, f_close_jQ, t_close_jQ;
    var dataFriend;
    var WORD_LIMIT = 150;

    this.create = function(_elm) {
        comment_jQ = $(_elm);
        input_jQ = $('.e_input', _elm);
        remain_jQ = $('.e_remain', _elm);
        publish_jQ = $('.e_publish', _elm);
        count_jQ = $('.e_count', _elm);
        submit_jQ = $('.e_submit', _elm);
        cancel_jQ = $('.e_cancel', _elm);
        op_emotion_jQ = $('.e_op_emotion', _elm);
        op_topic_jQ = $('.e_op_topic', _elm);
        op_friend_jQ = $('.e_op_friend', _elm);
        emotion_jQ = $('.e_emotion', _elm);
        topic_jQ = $('.e_topic', _elm);
        friend_jQ = $('.e_friend', _elm);

        //输入框相关事件
        input_jQ.keyup(function() {
            var inputLen = input_jQ.val().length;

            count_jQ.text(inputLen);

            if (inputLen > WORD_LIMIT) {
                count_jQ.css('color', 'red');
            } else {
                count_jQ.css('color', '#033E46');
            }
        });

        //点击“朋友”
        op_friend_jQ.click(function() {
            getFriendList('', '1');
            friend_jQ.toggle();
            if (emotion_jQ) { emotion_jQ.hide(); }
            if (topic_jQ) { topic_jQ.hide(); }
        });

        //点击“话题”
        op_topic_jQ.click(function() {
            getHotTopicList();
            topic_jQ.toggle();
            if (emotion_jQ) { emotion_jQ.hide(); }
            if (friend_jQ) { friend_jQ.hide(); }
        });

        //点击“表情”
        op_emotion_jQ.click(function() {
            getEmotionList();
            emotion_jQ.toggle();
            if (friend_jQ) { friend_jQ.hide(); }
            if (topic_jQ) { topic_jQ.hide(); }
        });

        //点击取消按钮
        cancel_jQ.click(function() {
            input_jQ.val('');
            if (emotion_jQ) { emotion_jQ.hide(); }
            if (friend_jQ) { friend_jQ.hide(); }
            if (topic_jQ) { topic_jQ.hide(); }
        });
    };


    //显示好友列表
    var displayFriendList = function(_data) {
        var html = [];
        $.each(_data, function(i) {
            html.push("<li><a>" + _data[i].nk + "</a></li>");
        });
        friend_jQ.find('ul').html(html.join(''));
    }

    //获取好友列表
    var getFriendList = function(keyword, pageNo) {
        var html = [];
        html.push('	<a class="close e_f_close" title="关闭"></a>');
        html.push('<div class="hd"><input type="text" value="搜索" class="e_search"></div>');
        html.push('<ul></ul>');
        html.push('<div class="ft"> @朋友账号，他就能在[提到我的]页收到</div>');

        friend_jQ.html(html.join(''));
        search_jQ = $('.e_search', friend_jQ);
        f_close_jQ = $('.e_f_close', friend_jQ);

        var data = { "keyword": keyword, "pageNo": pageNo };
        Backend.findAtFriend(data, function(_resp) {
            if (Backend.pageBeanValidate(_resp)) {
                dataFriend = _resp.results;
                displayFriendList(dataFriend);
                bindActionFriend();
                bindActionSearch();
            } else {
                friend_jQ.find('ul').html("<li>无好友</li>");
            }
        }, function() {
            friend_jQ.find('ul').html("<li>无好友</li>");
        });
    };

    //获取热门话题
    var getHotTopicList = function() {
        var html = [];
        html.push('<a title="关闭" class="close e_t_close"></a>');
        html.push('<div class="hd"><a class="e_insert" title="插入话题">插入话题</a></div>');
        html.push('<p>热门话题：</p>');
        html.push('<ul></ul>');
        topic_jQ.html(html.join(''));

        insert_jQ = $('.e_insert', topic_jQ);
        t_close_jQ = $('.e_t_close', topic_jQ);

        Backend.hotTop(null, function(_resp) {
            if (Backend.pageBeanValidate(_resp)) {
                var results = _resp.results;
                var html = [];
                $.each(results, function(i) {
                    html.push("<li><a>" + results[i].name + "</a></li>");
                });
                topic_jQ.find('ul').html(html.join(''));
                bindActionTopic();
            } else {
                topic_jQ.find('ul').html("<li>无推荐话题</li>");
            }
        });

        bindActionTopic();
    };

    //设置头像格式，{filename:filetitle}
    var getEmotionList = function() {
        emotion_jQ.html("<ul></ul>");
        var html = [];
        $.each(Util.faces, function(_i, _face) {
            html.push('<li Other=' + _i + ' title=' + _i + '><img alt="face" src="' + MPT.Config['global_faces_path'] + '/' + _face + '.gif" /></li>');
        });
        emotion_jQ.find('ul').html(html.join(''));
        bindActionEmotion();
    };

    //添加“朋友”事件
    var bindActionFriend = function() {
        friend_jQ.find('ul li').unbind("click");
        friend_jQ.find('ul li').bind("click", function() {
            var name = $(this).text();
            var txtcontent = input_jQ.val();
            var atName = "@" + name;
            if ((txtcontent + atName).length <= WORD_LIMIT) {
                Util.insertText(input_jQ.get(0), atName + " ");
            }
        });
    };

    var bindActionSearch = function() {
        search_jQ.blur(function() {
            if (this.value == '') {
                this.value = '搜索';
            }
        }).focus(function() {
            if (this.value == '搜索') {
                this.value = '';
            }
        }).keyup(function(e) {
            var _dataTemp = [];
            var key = this.value;
            if (key != "" && key != null) {
                $.each(dataFriend, function(i) {
                    var _temp = dataFriend[i];
                    var nk = _temp.nk;
                    var nfl = _temp.nfl;
                    var np = _temp.np;
                    if (nfl.indexOf(key) == 0 || nk.indexOf(key) == 0 || np.indexOf(key) == 0) {
                        _dataTemp.push(_temp);
                    }
                });
                displayFriendList(_dataTemp);
                bindActionFriend();
            } else {
                displayFriendList(dataFriend);
                bindActionFriend();
            }
            _dataTemp = null;

            e.stopPropagation();
        });

        f_close_jQ.click(function() {
            friend_jQ.hide();
        });
    }


    //添加“话题”事件 
    var bindActionTopic = function() {
        insert_jQ.click(function() {
            input_jQ.val("#在这里输入你想要说的话题# ");
        });

        topic_jQ.find('ul a').unbind("click");
        topic_jQ.find('ul a').bind("click", function() {
            var name = $(this).text();
            input_jQ.val("#" + name + "# ");
        });

        t_close_jQ.click(function() {
            topic_jQ.hide();
        });
    };

    //添加“表情”事件 
    var bindActionEmotion = function() {

        emotion_jQ.find('ul li').unbind("click");
        emotion_jQ.find('ul li').bind("click", function() {
            var id = $(this).attr("Other");
            var faceText = '[em:' + id + ']';

            var txtcontent = input_jQ.val();

            if (txtcontent == "今天心情如何？说上一句！") {
                txtcontent = "";
            }

            if ((txtcontent + faceText).length <= WORD_LIMIT) {					//添加表情到文本域中
                Util.insertText(input_jQ.get(0), faceText);
            }
        });
    };
});